package me.helllp.book.concurrent.ch03;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 重入锁的等待
 * 
 * @author Administrator
 *
 */
public class TimeLock implements Runnable{
	public static ReentrantLock lock = new ReentrantLock();

	@Override
	public void run() {
		try {
			if(lock.tryLock(5, TimeUnit.SECONDS)){
				Thread.sleep(6000);
			}else{
				System.out.println("获得锁失败！");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(lock.isHeldByCurrentThread()){
				lock.unlock();
			}
		}
	}
	
	public static void main(String[] args) {
		TimeLock obj = new TimeLock();
		
		Thread t1 = new Thread(obj);
		Thread t2 = new Thread(obj);
		
		t1.start();
		t2.start();
	}
}
